package com.z2wenfa.innosticproducer.controller;

import com.alibaba.fastjson.JSON;
import com.z2wenfa.innosticproducer.dao.InitDao;
import com.z2wenfa.innosticproducer.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@RestController
public class initController {

    @Autowired
    private InitDao initDao;


    @RequestMapping("/")
    @ResponseBody
    public String sayHello() {
        return "Hello World";
    }


    @RequestMapping("/initCompanies")
    @ResponseBody
    public String initCompany() {
        List<String> companyNameList = new ArrayList<>();
        companyNameList.add("北京");
        companyNameList.add("南京");
        companyNameList.add("上海");

        initDao.insertCompanyBatch(companyNameList);
        return JSON.toJSONString(companyNameList);
    }

    @RequestMapping(value = "/insertCompany/{name}")
    @ResponseBody
    public String insertCompany(@PathVariable("name") String name) {
        initDao.insertCompany(name);
        return name;
    }

    @ResponseBody
    @RequestMapping(value = "/findCompanyByName/{name}")
    public String findCompanyByName(@PathVariable("name") String name) {
        Company company = initDao.findCompanyByName(name);
        return JSON.toJSONString(company);
    }

    @ResponseBody
    @RequestMapping("/companyList")
    public String companyList() {
        List<Company> companyList = initDao.companyList();
        return JSON.toJSONString(companyList);
    }

    @ResponseBody
    @RequestMapping("/initServices")
    public String initService() {
        List<Company> companyList = initDao.companyList();
        List<Service> services = new ArrayList<>();

        int cout = 10;
        for (Company company : companyList) {
            for (int i1 = 0; i1 < 10; i1++) {
                Service service = new Service();
                service.setCompanyId(company.getId());
                service.setName("TZC0" + cout);
                services.add(service);
                cout++;
            }
        }

        initDao.insertServiceBatch(services);

        return "业务单元插入成功:" + JSON.toJSONString(services);
    }

    @RequestMapping("/initHospital")
    public String initHospital() {
        List<Service> serviceList = initDao.serviceList();
        List<Hospital> hospitals = new ArrayList<>();
        int count = 1;
        for (Service service : serviceList) {
            for (int i = 0; i < 10; i++) {
                Hospital hospital = new Hospital();
                hospital.setName("测试医院" + count);
                hospital.setServiceId(service.getId());
                hospitals.add(hospital);
                count++;
            }
        }
        initDao.insertHospitalBatch(hospitals);
        return "医院插入成功:" + JSON.toJSONString(hospitals);
    }


    @ResponseBody
    @RequestMapping("/initYear")
    public String initYear() {
        List<String> year = new ArrayList<>();
        year.add("2017");
        year.add("2016");
        year.add("2015");

        initDao.insertYearBatch(year);
        return "年份插入成功" + JSON.toJSONString(year);
    }

    @ResponseBody
    @RequestMapping("/initMonth")
    public String initMonth() {
        List<String> months = new ArrayList<>();
        months.add("一月");
        months.add("二月");
        months.add("三月");
        months.add("四月");
        months.add("五月");
        months.add("六月");
        months.add("七月");
        months.add("八月");
        months.add("九月");
        months.add("十月");
        months.add("十一月");
        months.add("十二月");

        initDao.insertMonthBatch(months);

        return "月份插入成功!";
    }

    @RequestMapping("/hospitalList")
    @ResponseBody
    public String hospitalList() {
        List<Hospital> hospitalList = initDao.hospitalList();

        return JSON.toJSONString(hospitalList);
    }

    @RequestMapping("/initSales")
    @ResponseBody
    public String initSales() {
        List<Year> yearList = initDao.yearList();
        List<Month> monthList = initDao.monthList();
        List<Hospital> hospitals = initDao.hospitalList();

        List<Sales> salesList = new ArrayList<>();
        Random random = new Random();
        for (Year year : yearList) {
            for (Month month : monthList) {
                for (Hospital hospital : hospitals) {
                    Sales sales = new Sales();
                    sales.setMonthId(month.getId());
                    sales.setYearId(year.getId());
                    sales.setHospitalId(hospital.getId());
                    int salesNum = random.nextInt(1000) + 1000;
                    Float salesValueUnit = random.nextFloat() * 5;
                    int salesBackNum = random.nextInt(salesNum / 10);
                    Float salesValue = salesValueUnit * salesNum;
                    Float salesBackValue = salesValueUnit * salesBackNum;
                    sales.setSalesNum(salesNum);
                    sales.setSalesValue(salesValue);
                    sales.setSalesBackNum(salesBackNum);
                    sales.setSalesBackValue(salesBackValue);
                    salesList.add(sales);
                }
            }
        }

        initDao.insertSalesBatch(salesList);

        return JSON.toJSONString(salesList);
    }

    @RequestMapping("/initProductType")
    public String initProdutType() {
        List<ProductType> productTypeFirstLevelList = new ArrayList<>();

        productTypeFirstLevelList.add(new ProductType("6877介入器材"));
        productTypeFirstLevelList.add(new ProductType("6864医用卫生材料及敷料"));
        productTypeFirstLevelList.add(new ProductType("6815注射穿刺器械"));
        productTypeFirstLevelList.add(new ProductType("6821医用电子仪器设备"));
        productTypeFirstLevelList.add(new ProductType("6866医用高分子材料及制品"));
        productTypeFirstLevelList.add(new ProductType("6846植入材料和人工器官"));
        initDao.insertProductTypeBatch(productTypeFirstLevelList);


        List<ProductType> productTypeSecondLevelList = new ArrayList<>();

        for (int i = 0; i < productTypeFirstLevelList.size(); i++) {
            ProductType productType = productTypeFirstLevelList.get(i);

            switch (productType.getId()) {
                case 1:
                    productTypeSecondLevelList.add(new ProductType("血管内导管", 1));
                    productTypeSecondLevelList.add(new ProductType("导丝和管鞘", 1));
                    productTypeSecondLevelList.add(new ProductType("栓塞器材", 1));
                    break;
                case 2:
                    productTypeSecondLevelList.add(new ProductType("敷料、护创材料", 2));
                    productTypeSecondLevelList.add(new ProductType("可吸收性止血、防粘连材料", 2));
                    productTypeSecondLevelList.add(new ProductType("手术用品", 2));
                    break;
                case 3:
                    productTypeSecondLevelList.add(new ProductType("注射穿刺器械", 3));
                    break;
                case 4:
                    productTypeSecondLevelList.add(new ProductType("心电导联线", 4));
                    productTypeSecondLevelList.add(new ProductType("无创医用传感器", 4));
                    productTypeSecondLevelList.add(new ProductType("用于心脏的治疗、急救装置", 4));
                    break;
                case 5:
                    productTypeSecondLevelList.add(new ProductType("输液、输血器具及管路", 5));
                    productTypeSecondLevelList.add(new ProductType("导管、引流管", 5));
                    productTypeSecondLevelList.add(new ProductType("肠道插管", 5));
                    break;
                case 6:
                    productTypeSecondLevelList.add(new ProductType("支架", 6));
                    productTypeSecondLevelList.add(new ProductType("植入器材", 6));
                    productTypeSecondLevelList.add(new ProductType("植入性人工器官", 6));
                    break;
            }
        }
        initDao.insertProductTypeBatch(productTypeSecondLevelList);
        return JSON.toJSONString(productTypeFirstLevelList.addAll(productTypeSecondLevelList));
    }


    @RequestMapping("/initOrderItemAndDetail")
    public String initOrder() {
        List<ProductType> secondLevelProductTypeList = initDao.secondLevelProductTypeList();
        List<Company> companyList = initDao.companyList();
        List<Order> orderList = new ArrayList<>();
        List<OrderDetail> orderDetailList = new ArrayList<>();

        List<Year> yearList = initDao.yearList();
        List<Month> monthList = initDao.monthList();

        Random random = new Random();

        for (Year year : yearList) {
            for (Month month : monthList) {
                for (Company company : companyList) {
                    int orderNum = random.nextInt(20);
                    for (int i = 0; i < orderNum; i++) {
                        Order order = new Order();
                        order.setCompanyId(company.getId());
                        order.setMonthId(month.getId());
                        order.setYearId(year.getId());
                        orderList.add(order);
                    }
                }
            }
        }

        initDao.insertOrderBatch(orderList);


        for (Order order : orderList) {
            int orderDetailSize = random.nextInt(secondLevelProductTypeList.size() + 1);
            for (int i = 0; i < orderDetailSize; i++) {
                OrderDetail orderDetail = new OrderDetail();
                orderDetail.setItemId(order.getId());
                orderDetail.setProductTypeId(secondLevelProductTypeList.get(random.nextInt((secondLevelProductTypeList.size()))).getId());
                orderDetail.setNum(random.nextInt(20));
                orderDetailList.add(orderDetail);
            }
        }
        initDao.insertOrderDetailBatch(orderDetailList);

        return JSON.toJSONString(orderList);
    }
}
